<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,200分)- 最多获得的短信条数、云短信平台优惠活动（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某云短信厂商&#xff0c;为庆祝国庆&#xff0c;推出充值优惠活动。<br /> 现在给出客户预算&#xff0c;和优惠售价序列&#xff0c;求最多可获得的短信总条数。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行客户预算M&#xff0c;其中 0 ≤ M ≤ 10^6<br /> 第二行给出售价表&#xff0c; P1, P2, … Pn , 其中 1 ≤ n ≤ 100 ,<br /> Pi为充值 i 元获得的短信条数。1 ≤ Pi ≤ 1000 , 1 ≤ n ≤ 100</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>最多获得的短信条数</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">6<br /> 10 20 30 40 60</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">70</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">分两次充值最优&#xff0c; 1 元、 5 元各充一次。总条数 10 &#43; 60 &#61; 70</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">15<br /> 10 20 30 40 60 60 70 80 90 150         </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">210</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">分两次充值最优&#xff0c; 10 元 5 元各充一次&#xff0c;总条数 150 &#43; 60 &#61; 210</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题是完全背包问题。</p> 
<p>如果大家不是很清楚完全背包的求解&#xff0c;可以看<a href="https://blog.csdn.net/qfc_128220/article/details/128903531" title="算法设计 - 01背包问题的状态转移方程优化&#xff0c;以及完全背包问题_伏城之外的博客-CSDN博客">算法设计 - 01背包问题的状态转移方程优化&#xff0c;以及完全背包问题_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<p>本题中&#xff1a;</p> 
<ul><li>客户预算M相当于背包的承重&#xff0c;</li><li>出售价表&#xff1a;</li></ul> 
<ol><li>i 元相当于物品的重量&#xff0c;</li><li>Pi 短信条数相当于物品的价值</li></ol> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const m &#61; lines[0] - 0;
    const p &#61; lines[1].split(&#34; &#34;).map(Number);
    console.log(getResult(m, p));
    lines.length &#61; 0;
  }
});

function getResult(m, p) {
  const dp &#61; new Array(m &#43; 1).fill(0);

  for (let i &#61; 0; i &lt;&#61; p.length; i&#43;&#43;) {
    for (let j &#61; 0; j &lt;&#61; m; j&#43;&#43;) {
      if (i &#61;&#61; 0 || j &#61;&#61; 0 || j &lt; i) continue;
      dp[j] &#61; Math.max(dp[j], dp[j - i] &#43; p[i - 1]);
    }
  }

  return dp[m];
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int m &#61; Integer.parseInt(sc.nextLine());
    Integer[] p &#61;
        Arrays.stream(sc.nextLine().split(&#34; &#34;)).map(Integer::parseInt).toArray(Integer[]::new);

    System.out.println(getResult(m, p));
  }

  public static int getResult(int m, Integer[] p) {
    int[] dp &#61; new int[m &#43; 1];

    for (int i &#61; 0; i &lt;&#61; p.length; i&#43;&#43;) {
      for (int j &#61; 0; j &lt;&#61; m; j&#43;&#43;) {
        if (i &#61;&#61; 0 || j &#61;&#61; 0 || j &lt; i) continue;
        dp[j] &#61; Math.max(dp[j], dp[j - i] &#43; p[i - 1]);
      }
    }

    return dp[m];
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
m &#61; int(input())
p &#61; list(map(int, input().split()))


# 算法入口
def getResult(m, p):
    dp &#61; [0 for i in range(m &#43; 1)]

    for i in range(len(p) &#43; 1):
        for j in range(m &#43; 1):
            if i &#61;&#61; 0 or j &#61;&#61; 0 or j &lt; i:
                continue

            dp[j] &#61; max(dp[j], dp[j - i] &#43; p[i - 1])

    return dp[m]


# 算法调用
print(getResult(m, p))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>